package org.example.likou150.array;

/**
* https://leetcode.cn/problems/remove-duplicates-from-sorted-array/?envType=study-plan-v2&envId=top-interview-150
* */
public class RemoveDuplicates {
    public static void main(String[] args) {
        int[] nums=new int[]{1,1,2};
        RemoveDuplicates removeDuplicates=new RemoveDuplicates();
        int i = removeDuplicates.removeDuplicates(nums);
        System.out.println(i);
    }

    public int removeDuplicates(int[] nums) {
        int current=0;
        for(int i=1; i<nums.length; i++){
            if(nums[current]!=nums[i]){
                nums[++current]=nums[i];
            }
        }
        return current+1;
    }

    /**
    * 这样做有问题
    * 只是将removeDuplicates中方法变量nums指向了新数组，main中的nums并无改变
    * */
//    public int removeDuplicates(int[] nums) {
//        nums = Arrays.stream(nums).boxed().collect(Collectors.toSet()).stream().mapToInt(Integer::intValue).toArray();
//        return nums.length;
//    }
}
